//
//  HDTabBarController.swift
//  个人微博
//
//  Created by 爱生活Geek on 16/10/21.
//  Copyright © 2016年 爱生活. All rights reserved.
//

import UIKit

class HDTabBarController: UITabBarController {

    override func viewDidLoad() {
        
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setupChildControllers()
        
        setupBottomButton()
        
    }

    lazy var compostButton = { () -> UIButton in
    
        let compostButton = UIButton(type: .custom)
        
        compostButton.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        compostButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        compostButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        compostButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return compostButton
    }()
}
extension HDTabBarController{
    
    func setupChildControllers() {
        
        let array = [
            ["clsName":"HDHomeViewController","title":"首页","imageName":"home"],
            ["clsName":"HDMessageViewController","title":"消息","imageName":"message_center"],
            ["clsName":"UIViewController"],
            ["clsName":"HDDiscoverViewController","title":"发现","imageName":"discover"],
            ["clsName":"HDMeViewController","title":"我","imageName":"profile"],
        ]
        
        var arrayM = [UIViewController]();
        
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    // MARK: - 底部中间按钮
    func setupBottomButton() {
        
        tabBar.addSubview(compostButton)
        
        let count: CGFloat = CGFloat(childViewControllers.count)
        
        //  将向内缩进的宽度减少，能够让按钮的宽度变大，盖住容错点，防止穿帮
        let w = tabBar.bounds.width / count - 1
        
        //  水平往里缩进2个，竖直不变
        compostButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        compostButton.addTarget(self, action: #selector(compostClick), for: .touchUpInside)
    }
    
    @objc private func compostClick(){
        
        print(#function)
    }
    
    private func controller(dict:[String: String]) -> UIViewController{
        
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            else {
                return UIViewController()
        }
        
        // 创建视图控制器
        let vc = cls.init()
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_highlighted")?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        
        let nav = HDNavigationController(rootViewController: vc)
        
        return nav
    }

}
